package cn.itcast.order.pojo;

import org.apache.poi.ss.formula.functions.T;

/**
 * @author danDong
 * @date 2025-05-21 14:20:14
 */
public class Counter {
    private int count = 0;

    // 同步方法
    public synchronized void increment() {
        count++;
    }

    public int getCount() {
        return count;
    }

    public static void main(String[] args) throws InterruptedException {
        Counter counter = new Counter();

        // 创建多个线程来测试同步
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 1000; i++) {
                counter.increment();
            }
        });

        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 1000; i++) {
                counter.increment();
            }
        });

        t1.start();
        t2.start();

        t1.join();
        t2.join();

        // 输出最终结果，应该是2000
        System.out.println("Final count: " + counter.getCount());
    }
}
